<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="50%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="数据源类型" prop="sourceType">
            <el-select
              v-model="formData.sourceType"
              placeholder="请选择数据源类型"
              @change="handleChangeType"
              v-if="formData.id"
              disabled
            >
              <el-option
                v-for="(item, index) in sourceTypeDatas"
                :key="index"
                :label="item.dictLabel"
                :value="item.dictLabel"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="formData.sourceType"
              placeholder="请选择数据源类型"
              @change="handleChangeType"
              v-else
            >
              <el-option
                v-for="(item, index) in sourceTypeDatas"
                :key="index"
                :label="item.dictLabel"
                :value="item.dictLabel"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="数据源编码" prop="sourceCode">
            <el-input v-model="formData.sourceCode" placeholder="请输入" v-if="formData.id" disabled/>
            <el-input v-model="formData.sourceCode" placeholder="请输入" v-else/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="数据源名称" prop="sourceName">
            <el-input v-model="formData.sourceName" placeholder="请输入" v-if="formData.id" disabled />
            <el-input v-model="formData.sourceName" placeholder="请输入" v-else />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="数据源描述" prop="sourceDesc">
            <el-input
              v-model="formData.sourceDesc"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="isMysql">
        <el-row v-for="(item, index) in sourceConfig" :key="index">
          <el-col :span="24">
            <el-form-item :label="item.labelValue" :prop="item.label">
              <el-input :value="item.value" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button type="success" @click="testConnect">测试连接</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import { getDatasourceType, addDatasource, editDatasource, testConnection } from "@/api/aifactory";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      sourceTypeDatas: [], //数据源类型
      formData: {},
      sourceConfig: {},
      isMysql: false,
      // 表单校验
      rules: {
        sourceName: [
          { required: true, message: "数据源名称不能为空", trigger: "blur" },
        ],
        sourceCode: [
          { required: true, message: "数据源编码不能为空", trigger: "blur" },
        ],
        sourceType: [
          { required: true, message: "请选择数据源类型", trigger: "change" },
        ],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.formData = this.dialogData;
      //获取数据源
      getDatasourceType().then((res) => {
        if (res.code == 200) {
          for (let i in res.data) {
            if (res.data[i].dictLabel != "file") {
              res.data[i].sourceConfig = JSON.parse(res.data[i].dictValue);
            }
          }
          this.sourceTypeDatas = res.data;
        }
      });
      if (this.formData.id) {
        setTimeout(() => {
          this.handleChangeType(this.formData.sourceType);
        }, 500)
      }
    },
    handleChangeType(val) {
      for (let i in this.sourceTypeDatas) {
        if (this.sourceTypeDatas[i].dictLabel == val) {
          this.sourceConfig = this.sourceTypeDatas[i].sourceConfig;
        }
      }
      if (val != "file") {
        this.isMysql = true;
      }
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {
        sourceName: undefined,
        sourceCode: undefined,
        sourceType: undefined,
        sourceDesc: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (this.formData.sourceType != "file") {
          this.formData.sourceConfig = JSON.stringify(this.sourceConfig);
        } else {
          this.formData.sourceConfig = "{}";
        }
        if (valid) {
          if (this.formData.id != undefined) {
            editDatasource(this.formData).then((response) => {
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          } else {
            addDatasource(this.formData).then((response) => {
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
    // 测试连接
    testConnect() {
      const data = {sourceConfig: this.formData.sourceConfig, sourceType: this.formData.sourceType}
      testConnection(data).then(res => {
        console.log('testConnection', res)
      })
    },
  },
};
</script>
